package com.xx.text;

import com.xx.dao.auction.AuctionDao;
import com.xx.dao.auctionrecord.AuctionRecordDao;
import com.xx.entity.Auction;
import com.xx.entity.VO.AuctionRecordVo;
import com.xx.service.AuctionService;
import com.xx.service.Impl.AuctionServiceImpl;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 许欣
 * 时间：2022/4/13 9:57
 * 描述：
 */
public class AuctionTest {
    public static void main(String[] args) throws IOException {
        //添加
//        Auction auction=new Auction("国书1","50000","1000","2020-12-20","2020-11-20",null,null,null);
//        auction.setAuctionName("国书");
//        auction.setAuctionPic("50000");
//        auction.setAuctionUpset("1000");
//        auction.setAuctionStartTime("2020-12-20");
//        auction.setAuctionEndTime("2020-11-20");
//        auction.setAuctionDesc(null);

//        String resource = "mybatis-config.xml";
//        InputStream is = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        //UserDao userDao=sqlSession.getMapper(UserDao.class);
//        //AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
//        AuctionService dao=new AuctionServiceImpl();
//        int ret=dao.save(auction);
//        sqlSession.commit();
//        sqlSession.rollback();
//        sqlSession.close();
//        if(ret>0){
//            System.out.println("添加成功");
//        }else {
//            System.out.println("添加失败");
//        }

//
//
//        //修改
//        Auction auction=new Auction();
//        auction.setAuctionName("国书111");
//        auction.setAuctionStartPrice("20000");
//        auction.setAuctionUpset("2000");
//        auction.setAuctionStartTime("2020-12-20");
//        auction.setAuctionEndTime("2020-11-20");
//        auction.setAuctionDesc(null);
//        auction.setAuctionId(198);
//        String resource = "mybatis-config.xml";
//        InputStream is = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        //UserDao userDao=sqlSession.getMapper(UserDao.class);
//       // AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
//        AuctionService dao=new AuctionServiceImpl();
//        int ret=dao.update(auction);
//        sqlSession.commit();
//        sqlSession.rollback();
//        sqlSession.close();
//        if(ret>0){
//            System.out.println("修改成功");
//        }else {
//            System.out.println("修改失败");
//        }

//        String resource = "mybatis-config.xml";
//        InputStream is = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        //UserDao userDao=sqlSession.getMapper(UserDao.class);
//        //AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
//        AuctionService dao=new AuctionServiceImpl();
//        Auction ret=dao.getId(1);
//        System.out.println(ret.getAuctionId());

        //查询所有
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //UserDao userDao=sqlSession.getMapper(UserDao.class);
        //AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        AuctionService dao=new AuctionServiceImpl();
        List<Auction> ret=dao.getAll();
       for (Auction auction : ret){
           System.out.println(auction.getAuctionName());
       }

//        //删除
//        String resource = "mybatis-config.xml";
//        InputStream is = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        //UserDao userDao=sqlSession.getMapper(UserDao.class);
//        //AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
//        AuctionService dao=new AuctionServiceImpl();
//        int ret=dao.delete(192);
//        sqlSession.commit();
//        sqlSession.rollback();
//        sqlSession.close();
//        if (ret>0){
//            System.out.println("删除成功");
//        }else {
//            System.out.println("删除失败");
//        }
    }
    @Test
    public void get() throws IOException {
//        String resource = "mybatis-config.xml";
//        InputStream is = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        AuctionRecordDao dao=sqlSession.getMapper(AuctionRecordDao.class);
//        //AuctionRecordService dao=new AuctionRecordServiceImpl();
//        List<AuctionRecordVo> list=dao.getAll1(2);
//        System.out.println(list);
//        for (AuctionRecordVo auction:list){
//            System.out.println(auction.getAuctionName()+auction.getAuctionId()+auction.getAuctionStartPrice()+auction.getUserName());
//        }
//        String resource = "mybatis-config.xml";
//        InputStream is = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//       // AuctionRecordDao dao=sqlSession.getMapper(AuctionRecordDao.class);
//        AuctionRecordService dao=new AuctionRecordServiceImpl();
//        List<Auctionrecord> list=dao.getAllId();
//        for (Auctionrecord auction:list){
//            System.out.println(auction.getAuctionPrice()+"\t");
//        }
        //修改
//        Auctionrecord auction=new Auctionrecord();
//        auction.setId(null);
//        auction.setUserId(44);
//        auction.setAuctionId(196);
//        auction.setAuctionPrice("8000");
//        String resource = "mybatis-config.xml";
//        InputStream is = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        //UserDao userDao=sqlSession.getMapper(UserDao.class);
//        // AuctionRecordDao dao=sqlSession.getMapper(AuctionRecordDao.class);
//        AuctionRecordService dao=new AuctionRecordServiceImpl();
//        int ret=dao.add(auction);
//        sqlSession.commit();
//        sqlSession.rollback();
//        sqlSession.close();
//        if(ret>0){
//            System.out.println("修改成功");
//        }else {
//            System.out.println("修改失败");
//        }

                String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //AuctionDao dao=sqlSession.getMapper(AuctionDao.class);
        AuctionService dao=new AuctionServiceImpl();
        List<Auction> list=dao.getLike("花瓶");
        System.out.println(list);
    }
}
